//
//  RootViewController.m
//  二维码
//
//  Created by 绿水 on 16/3/16.
//  Copyright © 2016年 MJ. All rights reserved.
//

#import "RootViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface RootViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{
    
        AVCaptureSession *_session;
        AVCaptureDevice *_device;
        AVCaptureDeviceInput *_input;
        AVCaptureMetadataOutput *output;
        
    
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    
    
    
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
    
    output = [[AVCaptureMetadataOutput alloc]init];
    
    
    
    _session = [[AVCaptureSession alloc]init];
    
    if ([_session canAddInput:_input]) {
        
        [_session addInput:_input];
    }
    
    if ([_session canAddOutput:output]) {
        
        [_session addOutput:output];
    }
    
    CGFloat screenHigh = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    [output setRectOfInterest:CGRectMake(0, 0.5, 0.5,0.5)];
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode128Code]];
    
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    [_session setSessionPreset:AVCaptureSessionPreset1280x720];
    
    AVCaptureVideoPreviewLayer *_preView =   [AVCaptureVideoPreviewLayer layerWithSession:_session];
    
    _preView.videoGravity  = AVLayerVideoGravityResizeAspectFill;
    
    _preView.backgroundColor = [[UIColor redColor] CGColor];
    
    _preView.frame = self.view.layer.bounds;
    
    [self.view.layer insertSublayer:_preView above:0];
    [_session startRunning];
    

    
    
    
    
    UIView *maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    maskView.backgroundColor = [UIColor lightGrayColor];
    maskView.alpha = 0.5;
    [self.view addSubview:maskView];

    
    UILabel *lable = [[UILabel alloc]init];
    lable.text = @"河南机电";
    lable.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:lable];
    
    

}



-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    
    NSString *stringValue;
    
    if ([metadataObjects count] > 0) {
        
        [_session stopRunning];
    }
    
    AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
    
    
    stringValue = metadataObject.stringValue;
    
    
    NSLog(@"%@",stringValue);
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
